<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
    	if(IS_POST){
    		$username=$_POST['username'];
    		$p=$_POST['pass'];
			$pass=md5($p);
    		$m=M('userinfo');
			$ret=$m->field("id,username,nickname")->where("username='{$username}' and password='{$pass}'")->find();
			if($ret){
				session_start();
				$_SESSION['username']=$username;
				$_SESSION['id']=$ret["id"];
				//查询用户权限
				$rules=$this->getRules($ret["id"]);
				$ret['rules']=$rules;
				$this->ajaxReturn($ret);
			}else{
				$obj=(object)array();
				$obj->code=1;
				$obj->message="用户名或密码错误";
				$this->ajaxReturn($obj);
			}
		}else{
			$this -> display();
		}
    }
	public function error(){
		$this->display();
	}
	private function getRules($uid){
		$access=M('auth_group_access');
		$rules=$access->field("rules")->join('think_auth_group ON think_auth_group_access.group_id=think_auth_group.id')->where("uid=".$uid)->find();
		$rule=M('auth_rule');
		$userRules=$rule->field("id,name,title,pid")->where("id in(".$rules['rules'].") and is_menu =1")->select();
		return $userRules;
	}
}